home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Brought to you by... / Source-n-stuff / sample win.c < prev    next >
Encoding:
Text File  |  1997-06-28  |  8.8 KB  |  374 lines  |  [TEXT/CWIE]

  1. // File "sample win.c" -
  2.  
  3. #include "main.h"
  4. #include "floaters.h"
  5. #include "prefs.h"
  6. #include "sample drag.h"
  7. #include "sample win.h"
  8. #include <string.h>
  9. #include "Offscreen.h"
  10.  
  11. // ***********************************************************************************
  12. // Global Declarations 
  13.  
  14. extern GlobalsRec glob;
  15. WindowPtr    gAdWindow = nil;
  16. void ShowSponsorship(void);
  17. WindowPtr    gSplashWindow;
  18.  
  19. void ShowSplash(StringPtr name);
  20. void HideSplash(void);
  21. void SplashIdle();
  22.  
  23. // ***********************************************************************************
  24. // ***********************************************************************************
  25.  
  26. WindowPtr NewSampleWindow() {
  27.     static short count = 0, **prefCount;
  28.     Rect bounds; 
  29.     WindowPtr win;
  30.     Str255 textBuff = "\pSample Window ", altBuff;
  31.     StringPtr winTextPtr;
  32.  
  33.     // Demo the prefs functions by incrementing the Window #
  34.     if (prefCount = (short **) GetPrefs('WCnt', 1)) {
  35.         count = (**prefCount < 999) ? ++(**prefCount) : (**prefCount = 0);
  36.         WritePrefs((Handle) prefCount, 'WCnt', 1);
  37.         DisposeHandle((Handle) prefCount);
  38.         }
  39.       else if (prefCount = (short **) NewHandle(sizeof(**prefCount))) {
  40.         **prefCount = ++count;
  41.         WritePrefs((Handle) prefCount, 'WCnt', 1);
  42.         DisposeHandle((Handle) prefCount);
  43.         }
  44.       else count++;
  45.  
  46.     // Setup the Window Title String
  47.     NumToString(count, altBuff);
  48.     BlockMove(altBuff+1, textBuff + textBuff[0] + 1, altBuff[0]);
  49.     textBuff[0] += altBuff[0];
  50.  
  51.     
  52.     bounds = qd.screenBits.bounds;
  53.     //SetRect(&bounds, 100, 100, 320, 202);
  54.     //OffsetRect(&bounds, (count-1) % 3 * 40, (count-1) % 3 * 40);
  55.     gAdWindow = win = NewFloater(0, &bounds, textBuff, FALSE, kWDEFProcID, (WindowPtr) -1, TRUE, 0, '*923',
  56.             SampleWindowEventHandler, DisposeSampleWindow);
  57.  
  58.     // Apply some text to draw for update routines
  59.     if (win) SetSampleWindowText(win,"\p");
  60.         
  61.     if (win) {
  62.         UpdateFloater(win);    // Force an immediate draw
  63. //        if (glob.hasDragMgr) SetupDragHandlers(win);
  64.         }
  65.  
  66.     SetRect(&bounds, 100, 100, 400, 250);
  67.     gSplashWindow = win = NewFloater(0, &bounds, textBuff, FALSE, plainDBox, (WindowPtr) -1, TRUE, 0, '*923',
  68.             SampleWindowEventHandler, DisposeSampleWindow);
  69.  
  70.     
  71.     return(win);
  72.     }
  73.  
  74. // ***********************************************************************************
  75. // ***********************************************************************************
  76.  
  77. void DisposeSampleWindow(WindowPtr win) {
  78.     StringPtr winTextPtr;
  79.     
  80.     if (winTextPtr = (StringPtr) GetWRefCon(win)) DisposePtr((Ptr) winTextPtr);
  81.     DisposeFloater(win);
  82.     
  83.     // For a background only app, this will cause us to quit when the user
  84.     //   closes the last floater. Its your choice if you want this to happen.
  85.     if (glob.bkgdOnly && ! GetIndFloater(1, FALSE)) glob.quitting = TRUE;
  86.     }
  87.  
  88. // ***********************************************************************************
  89. // ***********************************************************************************
  90.  
  91. void SampleWindowEventHandler(EventRecord *theEvent, WindowPtr win) {
  92.     Rect destRect;
  93.     GrafPtr savePort;
  94.     StringPtr winTextPtr;
  95.  
  96.     switch(theEvent->what) {
  97.         case nullEvent:
  98.             break;
  99.         case mouseDown: {
  100.             // The Window part has already been found and forwarded to us
  101.             short thePart = theEvent->message;    
  102.             
  103.             switch(thePart) {
  104.                 case inContent:
  105.                     GetPort(&savePort);
  106.                     SetPort(win);
  107.  
  108.                     if (glob.hasColorQD)
  109.                         LMSetHiliteMode(LMGetHiliteMode() ^ (1L << hiliteBit));
  110.                     InvertRect(&win->portRect);
  111.  
  112.                     if (glob.hasDragMgr) SampleWindowDrag(theEvent, win);
  113.                       else while(StillDown());
  114.  
  115.                     if (glob.hasColorQD)
  116.                         LMSetHiliteMode(LMGetHiliteMode() ^ (1L << hiliteBit));
  117.                     InvertRect(&win->portRect);
  118.                     
  119.                     SetPort(savePort);
  120.                     break;
  121.                 case inGrow:
  122.                     break;
  123.                 case inGoAway:
  124.                     if (TrackGoAway(win, theEvent->where))
  125.                         DisposeSampleWindow(win);
  126.                     break;
  127.                 case inZoomIn:
  128.                     break;
  129.                 case inZoomOut:
  130.                     break;
  131.                 }
  132.             break;
  133.             }
  134.         case keyDown:
  135.         case autoKey:
  136.             break;
  137.         case updateEvt: {
  138.             GetPort(&savePort);
  139.             SetPort(win);
  140.             BeginUpdate(win);
  141.             
  142.             // Draw something cool!
  143.             destRect = win->portRect;
  144.             EraseRect(&destRect);
  145.             InsetRect(&destRect, 3, 2);
  146.             TextFont(1); TextSize(9);
  147.             if (winTextPtr = GetSampleWindowText(win)) 
  148.                 TextBox(winTextPtr+1, winTextPtr[0], &destRect, teFlushDefault);
  149.             TextFont(0); TextSize(0);
  150.             
  151.             EndUpdate(win);
  152.             SetPort(savePort);
  153.             break;
  154.             }
  155.         }
  156.     }
  157.  
  158. // ***********************************************************************************
  159. // ***********************************************************************************
  160.  
  161. void SetSampleWindowText(WindowPtr win, StringPtr winText) {
  162.     Boolean dirty = FALSE;
  163.     Ptr storage = (Ptr) GetWRefCon(win);
  164.     GrafPtr savePort;
  165.     
  166.     if (! storage) { storage = NewPtr(sizeof(Str255)); dirty = TRUE; }
  167.     if (! storage) return;
  168.     
  169.     if (winText && ! EqualString(winText, (StringPtr) storage, 0, 0)) dirty = TRUE;
  170.     BlockMove((winText && winText[0]) ? winText : "\p", storage, sizeof(Str255));
  171.     SetWRefCon(win, (long) storage);
  172.     
  173.     // Force a redraw after changing content
  174.     if (dirty) {
  175.         GetPort(&savePort);
  176.         SetPort(win);
  177.         InvalRect(&win->portRect);
  178.         SetPort(savePort);
  179.         }
  180.     }
  181.  
  182.  
  183. static short Rand(short range)
  184. {
  185.     return (Random () & 0x7fff) % range;
  186. }; /*Rand*/
  187.  
  188. Str255 message = "\pBrought to you by:";
  189.  
  190. void ShowSponsorship(void)
  191. {
  192.     GrafPtr    savePort;
  193.     short    countRes;
  194.     
  195.     GetPort(&savePort);
  196.     if(gAdWindow)
  197.     {
  198.         RGBColor    color = {0xffff, 0, 0};
  199.         Rect        drawRect, picRect;
  200.         WindowOffscreen    *offscreen;
  201.         PicHandle    pic;
  202.             
  203.         ShowWindow(gAdWindow);
  204.         SetPort(gAdWindow);
  205.         
  206.         offscreen = DrawOffscreen( nil, gAdWindow);
  207.         TextFont(helvetica);
  208.         TextSize(36);
  209.         TextFace(bold);
  210.         RGBForeColor(&color);
  211.         
  212.         drawRect = gAdWindow->portRect;
  213.         
  214.         drawRect.top += 25;
  215.         TETextBox( &(message[1]), message[0], &drawRect, teCenter);
  216.         
  217.         countRes = Count1Resources('PICT');
  218.         
  219.         pic = (PicHandle)Get1IndResource('PICT', Rand(countRes) + 1);
  220.         
  221.         if(pic)
  222.         {
  223.             short    top, left;
  224.             
  225.             picRect = (*pic)->picFrame;
  226.             
  227.             OffsetRect(&picRect, -picRect.left, -picRect.top);
  228.             
  229.             top = (gAdWindow->portRect.bottom - 72 - picRect.bottom);
  230.             top /= 2;
  231.             top += 72;
  232.             
  233.             left = gAdWindow->portRect.right - picRect.right;
  234.             left /= 2;
  235.             
  236.             OffsetRect(&picRect, left, top);
  237.             
  238.             DrawPicture(pic, &picRect);
  239.         }
  240.                 
  241.         DrawOnscreen(offscreen);
  242.     
  243.         {
  244.             long lastTick = TickCount() + 300;
  245.         
  246.             while(lastTick > TickCount())
  247.             {
  248.                 
  249.             }
  250.         }
  251.         HideWindow(gAdWindow);
  252.     }
  253.     SetPort(savePort);
  254. }
  255.  
  256.  
  257. static void CenterWindow(WindowPtr w, int top)
  258. {
  259.     Rect scr; Point p;
  260.     int rsize,size,margin,xoff,yoff;
  261.     GrafPtr    wMgrPort;
  262.  
  263.     
  264.     GetWMgrPort(&wMgrPort);
  265.     scr = wMgrPort->portRect;
  266.     SetPort(w);
  267.     p.h = w->portRect.left; p.v = w->portRect.top;
  268.     LocalToGlobal(&p);
  269.  
  270.     size = scr.right - scr.left;
  271.     rsize = w->portRect.right - w->portRect.left;
  272.     margin = size - rsize;
  273.     if (margin > 0) {
  274.         margin >>= 1;
  275.         p.h = scr.left + margin;
  276.         }
  277.     size = scr.bottom - scr.top;
  278.     rsize = w->portRect.bottom - w->portRect.top;
  279.     margin = size - rsize;
  280.     if (margin > 0) {
  281.         margin >>= 1;
  282.         p.v = scr.top + margin;
  283.     }
  284.     MoveWindow(w,p.h,top?top:p.v,FALSE);
  285.  
  286. }
  287.  
  288. PicHandle    gPicture = nil;
  289. Boolean        gUsedPicture = false;
  290. short        gPicIndex = 1;
  291. void SplashIdle()
  292. {
  293.     if(gUsedPicture || gPicture == nil)
  294.     {
  295.         if(gPicture)    DisposeHandle((Handle)gPicture);
  296.         gPicture = nil;
  297.         
  298.         gPicture = (PicHandle)Get1IndResource('PICT', gPicIndex++);
  299.         
  300.         if(!gPicture)
  301.         {
  302.             gPicIndex = 1;
  303.             gPicture = (PicHandle)Get1IndResource('PICT', gPicIndex++);
  304.         }
  305.         
  306.         if(gPicture)
  307.             DetachResource((Handle)gPicture);
  308.         
  309.         gUsedPicture = false;
  310.     }
  311. }
  312.  
  313. void ShowSplash(StringPtr name)
  314. {
  315.     GrafPtr    savePort;
  316.     Rect    textRect, picRect;
  317.     Str255    splashMessage = "\pSponsored by:";
  318.  
  319.     GetPort(&savePort);
  320.     textRect = gSplashWindow->portRect;
  321.     
  322.     if(gPicture)
  323.     {
  324.         picRect = (*gPicture)->picFrame;
  325.         OffsetRect(&picRect, -picRect.left, -picRect.top);
  326.         textRect.bottom = textRect.top + 24 + (picRect.bottom - picRect.top);
  327.         textRect.right = textRect.left + (picRect.right - picRect.left);
  328.         
  329.         SizeWindow(gSplashWindow,textRect.right,textRect.bottom,false);
  330.     }
  331.     CenterWindow(gSplashWindow, 0);
  332.     textRect = gSplashWindow->portRect;
  333.     
  334.     gUsedPicture = true;
  335.     
  336.     ShowWindow(gSplashWindow);
  337.     SetPort(gSplashWindow);
  338.     
  339.     if(gPicture)
  340.     {
  341.         OffsetRect(&picRect, 0, 24);
  342.         DrawPicture(gPicture, &picRect);
  343.     }
  344.     
  345.     textRect.bottom = textRect.top + 12;
  346.     TextFont(applFont);
  347.     TextSize(10);
  348.     TextFace(bold);
  349.     
  350.     TETextBox(&(name[1]), name[0], &textRect, teCenter);
  351.     
  352.     OffsetRect(&textRect, 0, 12);
  353.     TETextBox(&(splashMessage[1]), splashMessage[0], &textRect, teCenter);
  354.     
  355.     SetPort(savePort);
  356.  
  357. }
  358.  
  359. void HideSplash(void)
  360. {
  361.     HideWindow(gSplashWindow);
  362. }
  363.  
  364. // ***********************************************************************************
  365. // ***********************************************************************************
  366.  
  367. StringPtr GetSampleWindowText(WindowPtr win) {
  368.     Ptr storage = (Ptr) GetWRefCon(win);
  369.     StringPtr winText;
  370.  
  371.     winText = (storage && ((StringPtr) storage[0])) ? ((StringPtr) storage) : "\p<No Data>";
  372.     return(winText); 
  373.     }
  374.